<template>
  <div class="app-container">
    <el-form ref="dataForm" :rules="accountFormRules" :model="accountForm" label-position="left" label-width="140px">
      <el-form-item label="月结账号" prop="accountCode">
        <el-input v-model="accountForm.accountCode" />
      </el-form-item>
      <el-form-item label="快递公司" prop="companyID">
            <el-select v-model="accountForm.companyID" filterable remote :remote-method="loadCompanyList" placeholder="请选择">
              <el-option v-for="item in companyOptions" :key="item.ID" :label="item.companyName" :value="item.ID" />
            </el-select>
      </el-form-item>
      <el-form-item label="责任人" prop="employeeID">
            <el-select v-model="accountForm.employeeID" filterable remote :remote-method="loadEmployeeList" placeholder="请选择">
              <el-option v-for="item in employeeOptions" :key="item.ID" :label="item.fullname" :value="item.ID" />
            </el-select>
      </el-form-item>
      <el-form-item label="描述" prop="remark">
        <el-input v-model="accountForm.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="备注" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCannel()">
        取消
      </el-button>
      <el-button type="primary" @click="createData()">
        确认
      </el-button>
      <el-button v-if="accountForm.status.value === 5" type="danger" @click="handleDelete(accountForm)">
        删除
      </el-button>
    </div>
  </div>
</template>

<script>
import { saveAccount, removeAccountByID, getAccountDetail } from '@/api/logistics/account'
import {   getCompanyUsableList } from '@/api/logistics/company'
import {   getEmployeeUsable } from '@/api/mechanism/employee'
import waves from '@/directive/waves' // waves directive
import Upload from '@/components/Upload/SingleImage2' // secondary package based on el-pagination
import { } from '@/utils'

export default {
  name: 'ComplexTable',
  components: { Upload},
  directives: { waves },
  filters: {
  },
  data() {
    return {
      tableKey: 0,
      companyOptions:[],
      employeeOptions:[],
      accountForm: {
        code: '',
        remark: '',
        status: {
          label: '',
          value: 1
        }
      },
      accountFormRules: [],
      keywordFormHidden:true,
      keywordForm:{
        title:""
      }
    }
  },
  created() {
    let accountID = this.$route.query && this.$route.query.accountID
    if (accountID !== undefined) {
      accountID = parseInt(accountID)
      if (!isNaN(accountID)) {
        this.listLoading = true
        const formData = {
          'accountID': accountID
        }
        getAccountDetail(formData).then(response => {
          if (response.status === 200) {
            this.accountForm = response.data
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }
    }
    console.log(accountID)
  },
  methods: {
    handleCannel() {
      this.$router.push({ path: 'account' })
    },
    loadCompanyList:function(accountName){
      let _this = this;
      let formData = {}
      if(accountName != undefined){
        formData['keyword'] = accountName;
      }
      getCompanyUsableList(formData).then((res)=>{
        if(res.status == 200){
            _this.companyOptions = res.data.list;
        }
      })

    },
    loadEmployeeList:function(accountName){
      let _this = this;
      let formData = {}
      if(accountName != undefined){
        formData['keyword'] = accountName;
      }
      getEmployeeUsable(formData).then((res)=>{
        if(res.status == 200){
            _this.employeeOptions = res.data.list;
        }
      })

    },
    createData() {
      this.listLoading = true
      const accountForm = this.accountForm
      try {
          accountForm.status = accountForm.status.value
      } catch (error) {
        accountForm.status = 6
      }
      console.log(accountForm)
      if (accountForm.ID > 0) {
        accountForm.accountID = accountForm.ID
      }
      saveAccount(accountForm).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'account' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    handleDelete(row, index) {
      const fromdata = {
        'accountID': row.ID,
      }
      removeAccountByID(fromdata).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'account' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    }
  }
}
</script>
